package com.kaizzi.data.ppt.template;

import com.fasterxml.jackson.databind.JsonNode;
import com.kaizzi.data.common.been.NameDouble;
import com.kaizzi.data.common.been.SeriesData;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.StringUtil;
import com.kaizzi.data.jdl.service.LogService;
import com.kaizzi.data.ppt.TemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.sl.usermodel.TableCell;
import org.apache.poi.sl.usermodel.TableShape;
import org.apache.poi.xslf.usermodel.XSLFChart;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/27
 */
@Component
@Slf4j
public class Template7 {

    @Resource
    private TemplateUtil templateUtil;
    @Resource
    private LogService logService;

    //文字替换
    public void text(List<XSLFTextParagraph> textParagraphs,JsonNode data) {
        //冗余代码，对比后此种为最好解决方案
        if(textParagraphs==null||textParagraphs.size()==0){
            return;
        }
        for (XSLFTextParagraph textParagraph : textParagraphs) {
            if(textParagraph==null){
                continue;
            }
            List<XSLFTextRun> textRuns = textParagraph.getTextRuns();
            if(textRuns==null){
                continue;
            }
            for (XSLFTextRun textRun : textRuns) {
                if (textRun == null || textRun.getXmlObject()  == null){
                    return;
                }
                try {
                    if (textRun.getRawText().equals("7KEY-3")){
                        textRun.setText(data.get("dataPeriod").asText());
                        break;
                    }
                    if (textRun.getRawText().equals("7KEY-1")){
                        String title = data.get("title").asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                }catch (Exception e){
                    logService.setDataLogStatus("ppt-error");
                    log.error("页码:{},异常原因:{}",7,e);
                }
            }
        }
    }

    //折线图替换
    public void lineChart(CTPlotArea plot, JsonNode data, XSSFWorkbook workbook, XSSFSheet sheet, XSLFChart chart) {
        //获取数据
        List<SeriesData> seriesDatas;
        // 根据名称判断是哪个条形图
        String v = plot.getLineChartList().get(0).getSerList().get(0).getVal().getNumRef().getNumCache().getPtArray(0).getV();
        if (v.equals("12")){
            seriesDatas = getLine1(data);
            log.info("line chart :{}",seriesDatas);
            templateUtil.lineChart(plot,seriesDatas,workbook,sheet,chart,2);
        }else {
            seriesDatas = getLine2(data);
            log.info("line chart :{}",seriesDatas);
            templateUtil.lineChart2(plot,seriesDatas,workbook,sheet,chart,1);
        }
    }

    private List<SeriesData> getLine2(JsonNode data) {
        List<SeriesData> seriesDatas = new ArrayList<>();
        List<NameDouble> creates = new ArrayList<>();
        List<NameDouble> joins = new ArrayList<>();
        JsonNode jsonNode = data.get("tempPage7s");
        for (JsonNode node : jsonNode) {
            String date = node.get("date").get("value").asText();
//            String createTeamUser = node.get("createTeamUser").get("value").asText();
//            String joinTeamUser = node.get("joinTeamUser").get("value").asText();

            NameDouble nameDouble1 = new NameDouble();
            nameDouble1.setName(date);
            nameDouble1.setValue(StringUtil.doubleFormat(node.get("createTeamUser").get("value").asText()));
            creates.add(nameDouble1);

            NameDouble nameDouble2 = new NameDouble();
            nameDouble2.setName(date);
            nameDouble2.setValue(StringUtil.doubleFormat(node.get("joinTeamUser").get("value").asText()));
            joins.add(nameDouble2);
        }
        SeriesData seriesData1 = new SeriesData();
        SeriesData seriesData2 = new SeriesData();

        seriesData1.setName("成为队长");
        seriesData1.setValue(creates);

        seriesData2.setName("成为队员");
        seriesData2.setValue(joins);

        System.out.println(JsonUtil.serialize(joins));
        System.out.println(JsonUtil.serialize(creates));

        seriesDatas.add(seriesData1);
        seriesDatas.add(seriesData2);
        return seriesDatas;
    }

    private List<SeriesData> getLine1(JsonNode data) {
        List<SeriesData> seriesDatas = new ArrayList<>();
        List<NameDouble> nameDoubles = new ArrayList<>();
        JsonNode jsonNode = data.get("tempPage7s");
        for (JsonNode node : jsonNode) {
//            String exchangeUser = node.get("exchangeUser").get("value").asText();
            String date = node.get("date").get("value").asText();
            NameDouble nameDouble = new NameDouble();
            nameDouble.setName(date);
            nameDouble.setValue(StringUtil.doubleFormat(node.get("exchangeUser").get("value").asText()));
            nameDoubles.add(nameDouble);
        }
        SeriesData seriesData = new SeriesData();
        seriesData.setValue(nameDoubles);
        seriesData.setName("兑换人数");
        seriesDatas.add(seriesData);
        return seriesDatas;
    }

    public void barChart(CTPlotArea plot, JsonNode data, XSSFWorkbook workbook, XSSFSheet sheet, XSLFChart chart) {
        //获取柱形图数据1
        List<SeriesData> seriesDatas = getBarData(data);
        log.info("bar chart:{}",seriesDatas);
        templateUtil.barChart(plot,seriesDatas,workbook,sheet,chart);
    }

    private List<SeriesData> getBarData(JsonNode data) {
        JsonNode jsonNode = data.get("tempPage7s");
        List<SeriesData> seriesDatas = new ArrayList<>();
        List<NameDouble> nameDoubles1 = new ArrayList<>();
        List<NameDouble> nameDoubles2 = new ArrayList<>();

        for (JsonNode node : jsonNode) {
            String date = node.get("date").get("value").asText();
            NameDouble nameDouble1 = new NameDouble();
            nameDouble1.setName(date);
            nameDouble1.setValue(StringUtil.doubleFormat(node.get("newUser").get("value").asText()));
            nameDoubles1.add(nameDouble1);
            NameDouble nameDouble2 = new NameDouble();
            nameDouble2.setName(date);
            nameDouble2.setValue(StringUtil.doubleFormat(node.get("appShareUser").get("value").asText()));
            nameDoubles2.add(nameDouble2);
        }
        SeriesData seriesData1 = new SeriesData("新增用户",nameDoubles1,"");
        SeriesData seriesData2 = new SeriesData("分享人数",nameDoubles2,"");
        seriesDatas.add(seriesData1);
        seriesDatas.add(seriesData2);
        return seriesDatas;
    }

    //表格内容替换
    public void table(XSLFShape shape, JsonNode data) {
        TableShape tableShape = (TableShape)shape;
        int numberOfColumns = tableShape.getNumberOfColumns();
        int numberOfRows = tableShape.getNumberOfRows();
        //遍历获取每一个单元格
        for (int col =1 ;col < numberOfColumns; col++){
            for (int row = 0; row<numberOfRows; row++ ){
                TableCell cell = tableShape.getCell(row, col);
                //特殊行单独处理 -后两行特殊处理
                if (row > numberOfRows -3){
                    if (col ==1){
                        XSLFTextRun textRun = ((XSLFTextParagraph) tableShape.getCell(row, 1).getTextParagraphs().get(0)).getTextRuns().get(0);
                        textRun.setText(getSpecialData(data,row));
                    }
                    continue;
                }
                //正常处理
                if (cell.getTextParagraphs().size() > 0){
                    XSLFTextParagraph cellParagraphs = (XSLFTextParagraph)cell.getTextParagraphs().get(0);
                    if (cellParagraphs.getTextRuns().size()> 0){
                        cellParagraphs.getTextRuns().get(0).setText(getCellData(data,col,row));
                    }
                }
            }
        }
    }

    //zhengchang
    private String getCellData(JsonNode data, int col, int row) {
        String jsonKey;
        switch (row){
            case 0:
                jsonKey = "date";
                break;
            case 1:
                jsonKey = "uv";
                break;
            case 2:
                jsonKey = "pv";
                break;
            case 3:
                jsonKey = "totalUser";
                break;
            case 4:
                jsonKey = "newUser";
                break;
            case 5:
                jsonKey = "newVisitUser";
                break;
            case 6:
                jsonKey = "jumpOutRate";
                break;
            case 7:
                jsonKey = "activeUser";
                break;
            case 8:
                jsonKey = "appShareUser";
                break;
            case 9:
                jsonKey = "appShareCount";
                break;
            case 10:
                jsonKey = "clockInCount";
                break;
            case 11:
                jsonKey = "clockInUser";
                break;
            case 12:
                jsonKey = "createTeamUser";
                break;
            case 13:
                jsonKey = "joinTeamUser";
                break;
            case 14:
                jsonKey = "expendPoint";
                break;
            case 15:
                jsonKey = "getPoint";
                break;
            case 16:
                jsonKey = "orderUser";
                break;
            case 17:
                jsonKey = "exchangeUser";
                break;
            default:
                jsonKey = "";
                break;
        }
        String result = data.get("tempPage7s").get(col-1).get(jsonKey).get("value").asText();
        if (row == 6){
            return result + "%";
        }
        return result.equals("0")?"-":result;
    }

    //teshu
    private String getSpecialData(JsonNode data, int row) {
        String jsonKey;
        if (row == 18){
            jsonKey = "recentActivity";
        }else {
            jsonKey = "recentUpdate";
        }
        return data.get(jsonKey).asText();
    }

    //箭头方向设置
    public void simpleShape(int red, XSLFShape shape,JsonNode data) {
        if (red ==0 || red%10 !=0){
            return;
        }
        String jsonKey = "";
        switch (red){
            case 10:
                jsonKey = "newUser";
                break;
            case 20:
                jsonKey = "newVisitUser";
                break;
            case 30:
                jsonKey = "jumpOutRate";
                break;
            case 40:
                jsonKey = "activeUser";
                break;
            case 50:
                jsonKey = "appShareUser";
                break;
            case 60:
                jsonKey = "appShareCount";
                break;
            case 70:
                jsonKey = "clockInCount";
                break;
            case 80:
                jsonKey = "clockInUser";
                break;
            case 90:
                jsonKey = "createTeamUser";
                break;
            case 100:
                jsonKey = "joinTeamUser";
                break;
            case 110:
                jsonKey = "expendPoint";
                break;
            case 120:
                jsonKey = "getPoint";
                break;
            case 130:
                jsonKey = "orderUser";
                break;
            case 140:
                jsonKey = "exchangeUser";
                break;
            case 150:
                jsonKey = "totalUser";
                break;
            case 160:
                jsonKey = "pv";
                break;
            case 170:
                jsonKey = "uv";
                break;
            default:
                jsonKey = "";
                break;
        }
        int trend = getSimpleData(data,jsonKey);
        templateUtil.replaceSimpleShape(trend,shape);
    }

    //箭头数据获取
    private int getSimpleData(JsonNode data, String jsonKey) {
        if (jsonKey == null || jsonKey.equals("")){
            return 1;
        }
        return data.get("tempPage7s").get(3).get(jsonKey).get("trend").asInt();
    }
}
